package org.ghost.spring.cloud.provider.demo.controller;

import org.apache.commons.lang3.RandomUtils;
import org.ghost.spring.cloud.provider.demo.dto.RspDTO;
import org.ghost.spring.cloud.provider.demo.dto.weather.WeatherDTO;
import org.ghost.spring.cloud.provider.demo.util.JacksonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping(value = "demo")
public class DemoController extends BaseController {
    private final static Logger logger = LoggerFactory.getLogger(DemoController.class);

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public RspDTO get(@PathVariable String id) {
        try {
            Thread.sleep(RandomUtils.nextInt(1, 1500));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("*****DemoController.get通过id查询,id={}", id);
        return this.render(id);
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public RspDTO post(@RequestBody WeatherDTO reqDTO) {
        logger.info("*****DemoController.post添加数据,body={}", JacksonUtil.useDefaultMapper().toJson(reqDTO));
        return this.render(reqDTO);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public RspDTO put(@PathVariable String id, @RequestBody WeatherDTO reqDTO) {
        logger.info("*****DemoController.put更新数据,id={},body={}", id, JacksonUtil.useDefaultMapper().toJson(reqDTO));
        return this.render(reqDTO);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public RspDTO delete(@PathVariable String id) {
        logger.info("*****DemoController.get通过id删除数据,id={}", id);
        return this.render(id);
    }

    @RequestMapping(value = "test1", method = RequestMethod.GET)
    public RspDTO get1() throws ExecutionException, InterruptedException {
        return this.render("test1张三");
    }

    @RequestMapping(value = "test2", method = RequestMethod.GET)
    public RspDTO get2() throws ExecutionException, InterruptedException {
        return this.render("test2李四");
    }

    @RequestMapping(value = "test3", method = RequestMethod.GET)
    public RspDTO get3() {
        return this.render("test3王五");
    }

    @RequestMapping(value = "test4", method = RequestMethod.GET)
    public RspDTO get4() {
        return this.render("test4测试");
    }
}

